# -*- coding: utf-8 -*-
import logging
"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
# Import helpers as desired, or define your own, ie:
#from webhelpers.html.tags import checkbox, password
from routes import url_for
from webhelpers.html.tags import *
from webhelpers.html import literal
from pythonblog.lib import auth
from pylons import request
import datetime

log = logging.getLogger(__name__)

domain = '.test.com'  #TODO: change this for actual domain
home = 'www' + domain

_date_format = '%Y-%m-%d'

def now():
    return datetime.datetime.now()

def get_readable_date(date):
    delta = now() - date
    days = delta.days
    if days >= 7:
        return datetime.datetime.strftime(date, _date_format)
    elif days >= 1:
        hours = delta.seconds / 3600
        minuts = (delta.seconds - hours * 3600) / 60
        return u'%s天%s时%s分之前' % (days, hours, minuts)
    else:
        hours = delta.seconds / 3600
        minuts = (delta.seconds - hours * 3600) / 60
        seconds = delta.seconds - hours * 3600 - minuts * 60
        if hours:
            return u'%s时%s分%s秒之前' % (hours, minuts, seconds)
        else:
            return u'%s分%s秒之前' % (minuts, seconds)

def url_for_www(*args, **kargs):
    if not kargs:
        kargs = {}
    kargs['sub_domain'] = 'www'
    return url_for(*args, **kargs)

def url_for_home():
    return url_for_www('home')

def get_path(menu, part_path, level=1):
    path = ''

    if level == 0:
        path = _time_duration(path, menu.time_duration)
        path = _rank_type(path, menu.rank_type)
        path = _post_type(path, menu.post_type)
        path = _section_path(path, menu.section_path)

    if level == 1:
        path = _time_duration(path, menu.time_duration)
        path = _rank_type(path, menu.rank_type)
        path = _post_type(path, menu.post_type)
        path = _section_path(path, part_path)

    if level == 2:
        path = _time_duration(path, menu.time_duration)
        path = _rank_type(path, menu.rank_type)
        path = _post_type(path, part_path)
        path = _section_path(path, menu.section_path)


    if level == 3:
        path = _time_duration(path, menu.time_duration)
        path = _rank_type(path, part_path)
        path = _post_type(path, menu.post_type)
        path = _section_path(path, menu.section_path)

    if level == 4:
        path = _time_duration(path, part_path)
        path = _rank_type(path, menu.rank_type)
        path = _post_type(path, menu.post_type)
        path = _section_path(path, menu.section_path)

    return path

def _time_duration(path, part_path):
    return _compose_path(path, part_path, 'recent')

def _rank_type(path, part_path):
    return _compose_path(path, part_path, 'new')

def _post_type(path, part_path):
    return _compose_path(path, part_path, 'all')

def _section_path(path, part_path):
    return _compose_path(path, part_path, 'all')

def _compose_path(path, part_path, default):
    if path:
        path = '/' + part_path + path
    elif part_path != default:
        path = '/' + part_path
    return path


def get_user_identifier():
    if request.environ.has_key('REMOTE_USER'):
        return request.environ['REMOTE_USER']
    else:
        #log.error(request.environ)
        return request.environ['REMOTE_ADDR']

from paste.request import construct_url
def get_current_url():
    return construct_url(request.environ)

def get_user_link(userInfo):
    if userInfo:
        return u'<a href="%s">%s</a>'%(url_for('home', sub_domain=userInfo.userName), userInfo.author)
    else:
        return u'游客'

